<!DOCTYPE HTML>
<html lang="en" >
    
    <head>
        
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <title>协程的实现之原语操作 | 协程</title>
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <meta name="description" content="">
        <meta name="generator" content="GitBook 2.6.7">
        
        
        <meta name="HandheldFriendly" content="true"/>
        <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
        <meta name="apple-mobile-web-app-capable" content="yes">
        <meta name="apple-mobile-web-app-status-bar-style" content="black">
        <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
        <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">
        
    <link rel="stylesheet" href="../gitbook/style.css">
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-highlight/website.css">
        
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-search/search.css">
        
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-fontsettings/website.css">
        
    
    

        
    
    
    <link rel="next" href="../chapter5/5_0.html" />
    
    
    <link rel="prev" href="../chapter3/3_0.html" />
    

        
    </head>
    <body>
        
        
    <div class="book"
        data-level="4"
        data-chapter-title="协程的实现之原语操作"
        data-filepath="chapter4/4_0.md"
        data-basepath=".."
        data-revision="Sat Oct 13 2018 10:54:16 GMT+0800 (CST)"
        data-innerlanguage="">
    

<div class="book-summary">
    <nav role="navigation">
        <ul class="summary">
            
            
            
                
                <li>
                    <a href="http://www.0voice.com" target="blank" class="custom-link">ntyTcp</a>
                </li>
            
            

            
            <li class="divider"></li>
            

            
    
        <li class="chapter " data-level="0" data-path="index.html">
            
                
                    <a href="../index.html">
                
                        <i class="fa fa-check"></i>
                        
                        前言
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1" data-path="chapter1/1_0.html">
            
                
                    <a href="../chapter1/1_0.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.</b>
                        
                        协程的起源
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2" data-path="chapter2/2_0.html">
            
                
                    <a href="../chapter2/2_0.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.</b>
                        
                        协程的案例
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3" data-path="chapter3/3_0.html">
            
                
                    <a href="../chapter3/3_0.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.</b>
                        
                        协程的实现之工作原理
                    </a>
            
            
        </li>
    
        <li class="chapter active" data-level="4" data-path="chapter4/4_0.html">
            
                
                    <a href="../chapter4/4_0.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.</b>
                        
                        协程的实现之原语操作
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5" data-path="chapter5/5_0.html">
            
                
                    <a href="../chapter5/5_0.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.</b>
                        
                        协程实现之切换
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6" data-path="chapter6/6_0.html">
            
                
                    <a href="../chapter6/6_0.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.</b>
                        
                        协程实现之定义
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="7" data-path="chapter7/7_0.html">
            
                
                    <a href="../chapter7/7_0.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.</b>
                        
                        协程实现之调度器
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="8" data-path="chapter8/8_0.html">
            
                
                    <a href="../chapter8/8_0.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>8.</b>
                        
                        协程性能测试
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="9" data-path="chapter9/9_0.html">
            
                
                    <a href="../chapter9/9_0.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>9.</b>
                        
                        协程多核模式
                    </a>
            
            
        </li>
    


            
            <li class="divider"></li>
            <li>
                <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
                    Published with GitBook
                </a>
            </li>
            
        </ul>
    </nav>
</div>

    <div class="book-body">
        <div class="body-inner">
            <div class="book-header" role="navigation">
    <!-- Actions Left -->
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="../" >协程</a>
    </h1>
</div>

            <div class="page-wrapper" tabindex="-1" role="main">
                <div class="page-inner">
                
                
                    <section class="normal" id="section-">
                    
                        <h1 id="4&#x534F;&#x7A0B;&#x7684;&#x5B9E;&#x73B0;&#x4E4B;&#x539F;&#x8BED;&#x64CD;&#x4F5C;">4.&#x534F;&#x7A0B;&#x7684;&#x5B9E;&#x73B0;&#x4E4B;&#x539F;&#x8BED;&#x64CD;&#x4F5C;</h1>
<p>&#x2003;&#x2003;<strong>&#x95EE;&#x9898;&#xFF1A;&#x534F;&#x7A0B;&#x7684;&#x5185;&#x90E8;&#x539F;&#x8BED;&#x64CD;&#x4F5C;&#x6709;&#x54EA;&#x4E9B;&#xFF1F;&#x5206;&#x522B;&#x5982;&#x4F55;&#x5B9E;&#x73B0;&#x7684;&#xFF1F;</strong><br>&#x2003;&#x2003;&#x534F;&#x7A0B;&#x7684;&#x6838;&#x5FC3;&#x539F;&#x8BED;&#x64CD;&#x4F5C;&#xFF1A;create, resume, yield&#x3002;&#x534F;&#x7A0B;&#x7684;&#x539F;&#x8BED;&#x64CD;&#x4F5C;&#x6709;create&#x600E;&#x4E48;&#x6CA1;&#x6709;exit&#xFF1F;&#x4EE5;NtyCo&#x4E3A;&#x4F8B;&#xFF0C;&#x534F;&#x7A0B;&#x4E00;&#x65E6;&#x521B;&#x5EFA;&#x5C31;&#x4E0D;&#x80FD;&#x6709;&#x7528;&#x6237;&#x81EA;&#x5DF1;&#x9500;&#x6BC1;&#xFF0C;&#x5FC5;&#x987B;&#x5F97;&#x4EE5;&#x5B50;&#x8FC7;&#x7A0B;&#x6267;&#x884C;&#x7ED3;&#x675F;&#xFF0C;&#x5C31;&#x4F1A;&#x81EA;&#x52A8;&#x9500;&#x6BC1;&#x534F;&#x7A0B;&#x7684;&#x4E0A;&#x4E0B;&#x6587;&#x6570;&#x636E;&#x3002;&#x4EE5;_exec&#x6267;&#x884C;&#x5165;&#x53E3;&#x51FD;&#x6570;&#x8FD4;&#x56DE;&#x800C;&#x9500;&#x6BC1;&#x534F;&#x7A0B;&#x7684;&#x4E0A;&#x4E0B;&#x6587;&#x4E0E;&#x76F8;&#x5173;&#x4FE1;&#x606F;&#x3002;co-&gt;func(co-&gt;arg) &#x662F;&#x5B50;&#x8FC7;&#x7A0B;&#xFF0C;&#x82E5;&#x7528;&#x6237;&#x9700;&#x8981;&#x957F;&#x4E45;&#x8FD0;&#x884C;&#x534F;&#x7A0B;&#xFF0C;&#x5C31;&#x5FC5;&#x987B;&#x8981;&#x5728;func&#x51FD;&#x6570;&#x91CC;&#x9762;&#x5199;&#x5165;&#x5FAA;&#x73AF;&#x7B49;&#x64CD;&#x4F5C;&#x3002;&#x6240;&#x4EE5;NtyCo&#x91CC;&#x9762;&#x6CA1;&#x6709;&#x5B9E;&#x73B0;exit&#x7684;&#x539F;&#x8BED;&#x64CD;&#x4F5C;&#x3002;<br>create&#xFF1A;&#x521B;&#x5EFA;&#x4E00;&#x4E2A;&#x534F;&#x7A0B;&#x3002;  </p>
<ol>
<li>&#x8C03;&#x5EA6;&#x5668;&#x662F;&#x5426;&#x5B58;&#x5728;&#xFF0C;&#x4E0D;&#x5B58;&#x5728;&#x4E5F;&#x521B;&#x5EFA;&#x3002;&#x8C03;&#x5EA6;&#x5668;&#x4F5C;&#x4E3A;&#x5168;&#x5C40;&#x7684;&#x5355;&#x4F8B;&#x3002;&#x5C06;&#x8C03;&#x5EA6;&#x5668;&#x7684;&#x5B9E;&#x4F8B;&#x5B58;&#x50A8;&#x5728;&#x7EBF;&#x7A0B;&#x7684;&#x79C1;&#x6709;&#x7A7A;&#x95F4;pthread_setspecific&#x3002;  </li>
<li>&#x5206;&#x914D;&#x4E00;&#x4E2A;coroutine&#x7684;&#x5185;&#x5B58;&#x7A7A;&#x95F4;&#xFF0C;&#x5206;&#x522B;&#x8BBE;&#x7F6E;coroutine&#x7684;&#x6570;&#x636E;&#x9879;&#xFF0C;&#x6808;&#x7A7A;&#x95F4;&#xFF0C;&#x6808;&#x5927;&#x5C0F;&#xFF0C;&#x521D;&#x59CB;&#x72B6;&#x6001;&#xFF0C;&#x521B;&#x5EFA;&#x65F6;&#x95F4;&#xFF0C;&#x5B50;&#x8FC7;&#x7A0B;&#x56DE;&#x8C03;&#x51FD;&#x6570;&#xFF0C;&#x5B50;&#x8FC7;&#x7A0B;&#x7684;&#x8C03;&#x7528;&#x53C2;&#x6570;&#x3002;  </li>
<li>&#x5C06;&#x65B0;&#x5206;&#x914D;&#x534F;&#x7A0B;&#x6DFB;&#x52A0;&#x5230;&#x5C31;&#x7EEA;&#x961F;&#x5217; ready_queue&#x4E2D;<br>&#x2003;&#x2003;&#x5B9E;&#x73B0;&#x4EE3;&#x7801;&#x5982;&#x4E0B;&#xFF1A;  </li>
</ol>
<pre><code>int nty_coroutine_create(nty_coroutine **new_co, proc_coroutine func, void *arg) {

    assert(pthread_once(&amp;sched_key_once, nty_coroutine_sched_key_creator) == 0);
    nty_schedule *sched = nty_coroutine_get_sched();

    if (sched == NULL) {
        nty_schedule_create(0);

        sched = nty_coroutine_get_sched();
        if (sched == NULL) {
            printf(&quot;Failed to create scheduler\n&quot;);
            return -1;
        }
    }

    nty_coroutine *co = calloc(1, sizeof(nty_coroutine));
    if (co == NULL) {
        printf(&quot;Failed to allocate memory for new coroutine\n&quot;);
        return -2;
    }

    //
    int ret = posix_memalign(&amp;co-&gt;stack, getpagesize(), sched-&gt;stack_size);
    if (ret) {
        printf(&quot;Failed to allocate stack for new coroutine\n&quot;);
        free(co);
        return -3;
    }

    co-&gt;sched = sched;
    co-&gt;stack_size = sched-&gt;stack_size;
    co-&gt;status = BIT(NTY_COROUTINE_STATUS_NEW); //
    co-&gt;id = sched-&gt;spawned_coroutines ++;
co-&gt;func = func;

    co-&gt;fd = -1;
co-&gt;events = 0;

    co-&gt;arg = arg;
    co-&gt;birth = nty_coroutine_usec_now();
    *new_co = co;

    TAILQ_INSERT_TAIL(&amp;co-&gt;sched-&gt;ready, co, ready_next);

    return 0;
}
</code></pre><p>yield&#xFF1A; &#x8BA9;&#x51FA;CPU&#x3002;
<code>void nty_coroutine_yield(nty_coroutine *co)</code><br>&#x2003;&#x2003;&#x53C2;&#x6570;&#xFF1A;&#x5F53;&#x524D;&#x8FD0;&#x884C;&#x7684;&#x534F;&#x7A0B;&#x5B9E;&#x4F8B;<br>&#x2003;&#x2003;&#x8C03;&#x7528;&#x540E;&#x8BE5;&#x51FD;&#x6570;&#x4E0D;&#x4F1A;&#x7ACB;&#x5373;&#x8FD4;&#x56DE;&#xFF0C;&#x800C;&#x662F;&#x5207;&#x6362;&#x5230;&#x6700;&#x8FD1;&#x6267;&#x884C;resume&#x7684;&#x4E0A;&#x4E0B;&#x6587;&#x3002;&#x8BE5;&#x51FD;&#x6570;&#x8FD4;&#x56DE;&#x662F;&#x5728;&#x6267;&#x884C;resume&#x7684;&#x65F6;&#x5019;&#xFF0C;&#x4F1A;&#x6709;&#x8C03;&#x5EA6;&#x5668;&#x7EDF;&#x4E00;&#x9009;&#x62E9;resume&#x7684;&#xFF0C;&#x7136;&#x540E;&#x518D;&#x6B21;&#x8C03;&#x7528;yield&#x7684;&#x3002;resume&#x4E0E;yield&#x662F;&#x4E24;&#x4E2A;&#x53EF;&#x9006;&#x8FC7;&#x7A0B;&#x7684;&#x539F;&#x5B50;&#x64CD;&#x4F5C;&#x3002;  </p>
<p>&#x2003;&#x2003;resume&#xFF1A;&#x6062;&#x590D;&#x534F;&#x7A0B;&#x7684;&#x8FD0;&#x884C;&#x6743;&#x3002;<br>&#x2003;&#x2003;<code>int nty_coroutine_resume(nty_coroutine *co)</code><br>&#x2003;&#x2003;&#x53C2;&#x6570;&#xFF1A;&#x9700;&#x8981;&#x6062;&#x590D;&#x8FD0;&#x884C;&#x7684;&#x534F;&#x7A0B;&#x5B9E;&#x4F8B;
&#x2003;&#x2003;&#x8C03;&#x7528;&#x540E;&#x8BE5;&#x51FD;&#x6570;&#x4E5F;&#x4E0D;&#x4F1A;&#x7ACB;&#x5373;&#x8FD4;&#x56DE;&#xFF0C;&#x800C;&#x662F;&#x5207;&#x6362;&#x5230;&#x8FD0;&#x884C;&#x534F;&#x7A0B;&#x5B9E;&#x4F8B;&#x7684;yield&#x7684;&#x4F4D;&#x7F6E;&#x3002;&#x8FD4;&#x56DE;&#x662F;&#x5728;&#x7B49;&#x534F;&#x7A0B;&#x76F8;&#x5E94;&#x4E8B;&#x52A1;&#x5904;&#x7406;&#x5B8C;&#x6210;&#x540E;&#xFF0C;&#x4E3B;&#x52A8;yield&#x4F1A;&#x8FD4;&#x56DE;&#x5230;resume&#x7684;&#x5730;&#x65B9;&#x3002;</p>

                    
                    </section>
                
                
                </div>
            </div>
        </div>

        
        <a href="../chapter3/3_0.html" class="navigation navigation-prev " aria-label="Previous page: 协程的实现之工作原理"><i class="fa fa-angle-left"></i></a>
        
        
        <a href="../chapter5/5_0.html" class="navigation navigation-next " aria-label="Next page: 协程实现之切换"><i class="fa fa-angle-right"></i></a>
        
    </div>
</div>

        
<script src="../gitbook/app.js"></script>

    
    <script src="../gitbook/plugins/gitbook-plugin-search/lunr.min.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-search/search.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-sharing/buttons.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-fontsettings/buttons.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-livereload/plugin.js"></script>
    

<script>
require(["gitbook"], function(gitbook) {
    var config = {"theme-default":{"showLevel":false},"highlight":{},"search":{"maxIndexSize":1000000},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2},"livereload":{}};
    gitbook.start(config);
});
</script>

        
    </body>
    
</html>
